import io, { Socket } from 'socket.io-client';
import { GlobalStore } from "@/store";

export default class IoSocket {
    socket: Socket;
    constructor() {        
        this.socket = io('http://192.168.1.103:3333', {
            reconnection: true,
            reconnectionAttempts: 3,
            reconnectionDelay: 1000,
            query: {userInfo:JSON.stringify(GlobalStore().userInfo)}
        })

        this.socket.on("connect", () => {
            console.log("连接成功");
        })
        this.socket.on('disconnect', (reason) => {
            console.log(`连接断开: ${reason}`);
        });
        this.socket.on("error", err => {
            console.log("连接错误", err);

        })
    }
    connect() {
        this.socket.connect();
    }

    disconnect() {
        this.socket.disconnect();
    }

    emit(eventName: string, data?: any) {
        console.log("监听到时见");

        this.socket.emit(eventName, data)
    }

    on(eventName: string, callback: (data: any) => void) {
        this.socket.on(eventName, callback);
    }
}